import arcpy
import uuid

# 获取当前工程
aprx = arcpy.mp.ArcGISProject("CURRENT")

# 获取活动的地图或场景
active_map_or_scene = aprx.activeMap
# 查找对应名称的图层
target_layer_name = "梅川镇观山村_要修改"

target_layer = None
for lyr in active_map_or_scene.listLayers():
    if lyr.name == target_layer_name:
        target_layer = lyr
        break

if target_layer is None:
    print("图层未找到。")
    exit

geometry_property = [["x", "INSIDE_X"], ["y", "INSIDE_Y"]]
cgcs2000_sr = arcpy.SpatialReference("GCS_China_Geodetic_Coordinate_System_2000")
arcpy.management.CalculateGeometryAttributes(
    target_layer,
    geometry_property,
    coordinate_system = cgcs2000_sr
)

print("计算完成x、y")

geometry_property = [["面积", "AREA"]]
unit = "SQUARE_METERS"
cgcs2000_degree = arcpy.SpatialReference("CGCS2000_3_Degree_GK_CM_117E")
arcpy.management.CalculateGeometryAttributes(
    target_layer,
    geometry_property,
    area_unit = unit,
    coordinate_system = cgcs2000_degree
)

print("计算完成面积")

# 获取 uuid 字段
uuid_field = [field for field in arcpy.ListFields(target_layer) if field.name == "uuid"][0]

# 遍历图层中的所有记录
count = 0
with arcpy.da.UpdateCursor(target_layer, ["uuid"]) as cursor:
    for row in cursor:
        # 生成新的 UUID
        new_uuid = str(uuid.uuid4())
        
        # 更新 uuid 字段
        row[0] = new_uuid
        
        # 更新记录
        cursor.updateRow(row)
        count += 1

print(f"UUID 已成功更新！更新数为：'{count}'" )



